
from flask_restful import Resource, marshal, fields, reqparse, abort

from App.apis.admin.utils import login_required
from App.apis.api_constant import HTTP_OK
from App.models.cinema_admin.cinema_user_model import CinemaUser

cinema_user_fields = {
    "id":fields.Integer,
    "username":fields.String,
    "password":fields.String(attribute = "_password"),
    "phone":fields.String,
    "is_verify":fields.Boolean
}

single_cinema_user_fields = {
    "status": fields.Integer,
    "msg":fields.String,
    "data":fields.Nested(cinema_user_fields)
}

multi_cinema_user_fields = {
    "status": fields.Integer,
    "msg":fields.String,
    "data":fields.List(fields.Nested(cinema_user_fields))
}

class AdminCinemaUsersResource(Resource):
    @login_required
    def get(self):
        cinema_users = CinemaUser.query.all()
        data = {
            'status':HTTP_OK,
            'msg':'ok',
            'data':cinema_users     # 不能在这里marshal，应该把整个data marshal。单给‘data’marshal，前面的就不是迭代的了
        }
        return marshal(data , multi_cinema_user_fields)


parse = reqparse.RequestParser()
parse.add_argument("is_verify" , type = bool , required = True , help = '请提供操作')

class AdminCinemaUserResource(Resource):
    @login_required
    def get(self , id):
        cinema_users = CinemaUser.query.get(id)
        data = {
            'status':HTTP_OK,
            'msg':'ok',
            'data':cinema_users     # 不能在这里marshal，应该把整个data marshal。单给‘data’marshal，前面的就不是迭代的了
        }
        return marshal(data , single_cinema_user_fields)

    # 通过patch修改表，让管理员能给cinema认证，完了才能在cinema_address_api里操作
    @login_required
    def patch(self , id):
        args = parse.parse_args()
        is_verify = args.get("is_verify")
        cinema_users = CinemaUser.query.get(id)
        cinema_users.is_verify = is_verify
        if not cinema_users.save():
            abort(400 , msg = '修改失败')

        data = {
            'status':HTTP_OK,
            'msg':'ok',
            'data':cinema_users     #???? 不能在这里marshal，应该把整个data marshal。单给‘data’marshal，前面的就不是迭代的了
        }
        return marshal(data , single_cinema_user_fields)

